from cachelib import SimpleCache

__cache = SimpleCache(default_timeout=12 * 60 * 60);


def get_value(key):
    return __cache.get(key)


def set_cache(key, value, timeout=None):
    """ 设置缓存,
    set 会直接覆盖已有. add 如果有, 则添加失败

    :param key:
    :param value:
    :param timeout:
    :return:
    """
    return __cache.set(key, value, timeout)


def delete(key):
    return __cache.delete(key)


def has(key, value=''):
    """ 先判断是否存在, 不存在则添加到缓存, 存在返回

    :param key:
    :param value:
    :return:
    """
    if __cache.has(key):
        _value = __cache.get(key)
        if value == _value:
            return True
    else:
        __cache.set(key, value)

    return False
